<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script type="importmap">
      {
        "imports": {
          "@testduet/wait-for": "https://esm.sh/@testduet/wait-for",
          "@testing-library/dom": "https://esm.sh/@testing-library/dom"
        }
      }
    </script>
    <script type="module">
      import { waitFor } from '@testduet/wait-for';
      import { queryAllByTestId } from '@testing-library/dom';

      const {
        testHelpers: { createDirectLineEmulator },
        WebChat: { createDirectLine, testIds }
      } = window;

      const { searchParams } = new URL(location.href);

      run(async function () {
        // We enable reduced motion by default, disable it first.
        await host.sendDevToolsCommand('Emulation.setEmulatedMedia', {
          features: [{ name: 'prefers-reduced-motion', value: '' }]
        });

        await host.sendDevToolsCommand('Emulation.setEmulatedMedia', {
          features: [{ name: 'prefers-colors-scheme', value: searchParams.get('theme') === 'dark' ? 'dark' : '' }]
        });

        const { directLine, store } = createDirectLineEmulator();

        WebChat.renderWebChat({ directLine, store }, document.getElementById('webchat'));

        await pageConditions.uiConnected();

        // WHEN: Upload button is clicked and a JPEG file is selected.
        await host.upload(pageElements.uploadButton(), 'seaofthieves.jpg');

        await waitFor(() => expect(queryAllByTestId(document, testIds.sendBoxAttachmentBarItem)).toHaveLength(1));

        // WHEN: Upload button is clicked and a second JPEG file is selected.
        await host.upload(pageElements.uploadButton(), 'forzahorizon5.jpg');

        await waitFor(() => expect(queryAllByTestId(document, testIds.sendBoxAttachmentBarItem)).toHaveLength(2));

        // WHEN: Upload button is clicked and a third JPEG file is selected.
        await host.upload(pageElements.uploadButton(), 'minecraftdungeons.jpg');

        await waitFor(() => expect(queryAllByTestId(document, testIds.sendBoxAttachmentBarItem)).toHaveLength(3));

        // THEN: With 3 attachments, they should appear as thumbnails.
        await host.snapshot('local');

        // WHEN: Reduced motions is enabled.
        await host.sendDevToolsCommand('Emulation.setEmulatedMedia', {
          features: [{ name: 'prefers-reduced-motion', value: 'reduce' }]
        });

        // THEN: Delete button should appear.
        await host.snapshot('local');
      });
    </script>
  </body>
</html>
